﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SicoMill : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_sicomill model = new tbl_sicomill();
        model.ID = Convert.ToInt32(id); ;

        BLL_tbl_sicomill bll = new BLL_tbl_sicomill();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                ViewState["ID"] = id;
                BLL_tbl_sicomill bll = new BLL_tbl_sicomill();
                List<tbl_sicomill> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_sicomill tbl in list)
                {
                    
                    ddlPO_Number.SelectedValue = tbl.PO_Number;
                    txtWork_Order_Number.Text = tbl.Work_Order_Number;
                    txtTotal_Input_RM_Si.Text = tbl.Total_Input_RM_Si.ToString();
                    ddlMill_Selection.SelectedValue = tbl.Mill_Selection.ToString();
                    txtRotor_RPM.Text = tbl.Rotor_RPM.ToString();
                    txtGas_Pressure.Text = tbl.Gas_Pressure.ToString();
                    txtFeed_rate.Text = tbl.Feed_rate.ToString();
                    txtTotal_Output_Product.Text = tbl.Total_Output_Product.ToString();
                    txtDate.Text = tbl.Date.ToString("yyyy-MM-dd");
                   
                }
                addInputDrums();
            }
        }
    }

    private void addInputDrums()
    {
        BLL_tbl_sicomill bll = new BLL_tbl_sicomill();
        int countInputvessels = bll.countSicomill_Inputvessels(Convert.ToInt32(ViewState["ID"]));
        int countOutputvessels = bll.countSicomill_Outputvessels(Convert.ToInt32(ViewState["ID"]));
        DataTable dtInput = bll.getDataSicomill_Inputvessels(Convert.ToInt32(ViewState["ID"]));
        DataTable dtOutput = bll.getDataSicomill_Outputvessels(Convert.ToInt32(ViewState["ID"]));
        string script = "";
        for (int i = 1; i <= countInputvessels; i++)
        {
            script += "$($($('#mytable1 tbody>tr:last>')).find('select')).val('" + dtInput.Rows[i - 1]["DRUM_BAG"] + "');  $($($('#mytable1 tbody>tr:last>')).find('input')).val('" + dtInput.Rows[i - 1]["DRUM_BAG_Number"] + "');";

            if (i < countInputvessels)
                script+="addRow('mytable1');";
        }

        for (int i = 1; i <= countOutputvessels; i++)
        {
            script += "$($($('#mytable2 tbody>tr:last>')).find('select')).val('" + dtOutput.Rows[i - 1]["DRUM_BAG"] + "');  $($($('#mytable2 tbody>tr:last>')).find('input')).val('" + dtOutput.Rows[i - 1]["DRUM_BAG_Number"] + "');";

            if (i < countOutputvessels)
                script += "addRow('mytable2');";
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), "AddingDrum", script, true);
    }


    protected void ddlPO_Number_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlPO_Number.Items.Insert(0,li);
    }
    protected void ddlBag_Drum_Input_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlBag_Drum_Input.Items.Insert(0, li);
    }
    protected void ddlMill_Selection_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlMill_Selection.Items.Insert(0, li);
    }
    protected void ddlBag_Drum_Output_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlBag_Drum_Output.Items.Insert(0, li);
    }
}